package com.ktjiaoyu.crm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class ShiroTester {

    @Test
    public void testShiroAuthentication(){
        //1、创建Realm(安全数据源)
        //通过shiro.ini配置文件创建Realm
        IniRealm realm = new IniRealm("classpath:shiro.ini");
        //2、配置SecurityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        //注入创建的Realm
        securityManager.setRealm(realm);
        SecurityUtils.setSecurityManager(securityManager);

        //3、获取Subject主体
        Subject subject = SecurityUtils.getSubject();
        //4、封装token
        UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
        try {
            //5、认证
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        System.out.println("是否认证通过："+subject.isAuthenticated());
        System.out.println("身份信息："+subject.getPrincipal());
    }

}
